﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml.Serialization;
namespace Week4_work1
{
   public class Program
    {
        static void Main(string[] args)
        {
            OrderService a = new OrderService();
             a.addOrder(0001, "苹果", "甲", 10);
             a.addOrder(0002, "香蕉", "乙", 8);
             a.addOrder(0003, "西瓜", "丙", 15);
              a.addOrder(0004, "凤梨", "丁", 13);
            // a.display();
            //  Console.WriteLine("删除一项后：");
            //  a.deleteOrder(a.searchByItem("西瓜"));
            // a.display();
            //  Console.WriteLine("修改一项后");
            //  a.rchangeOrder(a.searchByID(1), 0001, "葡萄", "甲", 20);
            //  a.display();
            //  Console.WriteLine();
            //  a.sort();
            //  Console.WriteLine("排序之后");
            //  a.display();
            // a.Import();
            a.Export();
            a.Import();
            a.display();
            a.addOrder(0005, "芒果", "戊", 26);        //添加
            Console.WriteLine();
            a.display();

            a.deleteOrder(a.searchByItem("西瓜"));       //删除
            Console.WriteLine();
            a.display();

            a.deleteOrder(a.searchByClient("己"));       //删除未成功
            Console.WriteLine();
            a.display();

            a.rchangeOrder(a.searchByID(0001), 0006, "橘子", "庚", 26);
            Console.WriteLine();
            a.display();

            a.rchangeOrder(a.searchByClient("甲"), 0007, "橙子", "辛", 69);
            Console.WriteLine();
            a.display();

            a.rchangeOrder(a.searchByItem("凤梨"), 0008, "梨子", "仁", 23);
                Console.WriteLine();
            a.display();

            a.rchangeOrder(a.searchByPrice(8), 0009, "桃子", "圭", 45);
            Console.WriteLine();
            a.display();

            a.rchangeOrder(a.searchByClient("我"), 0010, "米", "他", 70);
            a.sort();
            a.display();
            a.Export();
            a.Import();
        }
    }
    public class Order
    {
    public int ID;         //订单编号
    public int orderID
        {
            get { return ID; }
            set { if (value >= 0) { ID = value; }
                else { ID = 0; }
            }
        }
        public OrderDetails od = new OrderDetails();
     
        public override bool Equals(object obj)
        {
            Order a = obj as Order;
            return this.ID == a.ID;
        }
        public override int GetHashCode()
        {
            return ID;
        }
    }
    public class OrderDetails {
        public int price;
        public Client user = new Client();
        public OrderItem Item = new OrderItem();
        public int Price
        {
            get { return price; }
            set
            {
                if (value >= 0) { price = value; }
                else { price = 0; }
            }
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            return Convert.ToString(price);
        }

    }
    [Serializable]
    public class OrderService {
        public List<Order> ListOrder = new List<Order>();
        public void addOrder(int ID, string Item, string Client, int price){        //增加订单
            Order o = new Order();
            o.orderID = ID;
            o.od.Item.itemName = Item;
            o.od.user.clientName = Client;
            o.od.Price = price;
            bool same =false;
            foreach (Order n in ListOrder)
            {
                if (n.Equals(o))
                    same = true;
            }
            if (!same)
                ListOrder.Add(o);
        }
        public void deleteOrder(List<Order> ListDelete)                     {//删除订单
            if (ListOrder.Except(ListDelete).Count() < 1 || ListDelete.Count() == 0)
            {
                System.Console.WriteLine("订单删除失败");
                return;
            }
            ListOrder = ListOrder.Except(ListDelete).ToList();
        }

        public void rchangeOrder(List<Order> Listchange, int ID, string Itemname, string ClientN, int PriceN) {//修改订单
            if (ListOrder.Except(Listchange).Count() < 1 || Listchange.Count() == 0)
            {
                System.Console.WriteLine("订单修改失败");
                return;
            }
            ListOrder = ListOrder.Except(Listchange).ToList();
            foreach (Order n in Listchange)
            {
                n.orderID = ID;
                n.od.Item.itemName = Itemname;
                n.od.user.clientName = ClientN;
                n.od.Price = PriceN;
            }
            ListOrder = ListOrder.Union(Listchange).ToList();
        }

        public List<Order> searchByID(int id)            //通过单号查询
        {
            return ListOrder.Where(n => n.orderID == id).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public List<Order> searchByItem(string name)               //商品查询
        {
            return ListOrder.Where(n => n.od.Item.itemName== name).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public List<Order> searchByClient(string client)            //用户名查询
        {
            return ListOrder.Where(n => n.od.user.clientName.Equals(client)).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public List<Order> searchByPrice(int price)                 //价格查询
        {   
            return ListOrder.Where(n => n.od.Price ==price).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public void sort()                                                  //根据单号排序
        {
            ListOrder = ListOrder.OrderBy(n => n.orderID).ToList();
        }

        public void display()                                               //展示订单
        {
            foreach (Order n in ListOrder)
            {
               Console.WriteLine($"订单号：{n.orderID.ToString()} 客户：{n.od.user.ToString()} 金额：{n.od.ToString()} 商品名称：{n.od.Item.ToString()}");
            }
        }

        public void Export() {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            string XmlFileName = "order.xml";
            FileStream fileStream = new FileStream(XmlFileName, FileMode.Create);
            xmlSerializer.Serialize(fileStream, ListOrder);
            fileStream.Close();
            //string xml = File.ReadAllText(XmlFileName);
           // Console.WriteLine(xml);
        }

        public void Import() {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            string XmlFileName = "order.xml";
            FileStream fileStream = new FileStream(XmlFileName, FileMode.Open);
            ListOrder =
                xmlSerializer.Deserialize(fileStream) as List<Order>;
            fileStream.Close();
            string xml = File.ReadAllText(XmlFileName);
            Console.WriteLine(xml);

        }
    }
    public class Client {
        public string client;
        public string clientName
        {
            get { return client; }
            set {
                client = value;
            }
        }

        public override string ToString()
        {
            return client;
        }
    }
    public class OrderItem {
        public string item;
        public string itemName {
            get { return item; }
            set { item = value; }
        }
        public override string ToString()
        {
            return item;
        }
    }

    

}

